package day4_thread_safe.exclusive;

import java.util.concurrent.TimeUnit;

/**
 * 线程同步的现象
 */
public class Demo {

    // 定义一把锁
    static Object o = new Object();

    static class Thread1 extends Thread {
        @Override
        public void run() {
            synchronized (o) {
                // 休眠3秒，再输出
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("1.先拧开瓶盖");
            }
        }
    }

    static class Thread2 extends Thread {
        @Override
        public void run() {
            synchronized (o) {
                System.out.println("2.再喝口酸奶");
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread1();
        t1.start();
        Thread t2 = new Thread2();
        t2.start();
    }
}
